// pages/reward/reward.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rewardList: [],
    currentPage: 1,
    luckyList: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRewardListData()
    this.getLuckyListData()
  },
  getLuckyListData: function () {
    var that = this
    app.fetchPostApi('market/gashapon/luckList', {
      "id": '0'
    }).then(data => {
      that.setData({
        luckyList: data
      })
    })
  },
  getRewardListData: function() {
    var that = this
    app.fetchPostApi('market/gashapon/index', {
      "page": this.data.currentPage, 
      "size":'10'
    }).then(data => {

      var myDate = parseInt(Date.parse(new Date()).toString().substring(0, 10))
      var rewardData = that.data.rewardList
      //设置一个type 0--未开始 1--无结束的进行中 2--有结束的进行中 3--已结束
      for (var i = 0; i < data.length; i++) {

        //判断扭蛋机状态
        if (data[i].startTime > myDate) {//未开始
          data[i].type = 0
        }else {//开始
          if (data[i].startTime == -1 && data[i].endTime == -1) {//没有设置时间
            data[i].type = 1
          } else if (data[i].endTime == -1) {//判断开始时间或结束时间未设置的情况
            data[i].type = 1
          } else {
            if (data[i].endTime < myDate) {//已经结束
              data[i].type = 3
            }else {
              data[i].type = 2
            }
          }
        }





        //判断开始和结束时间是否同一天和同一年
        if (app.formatTime(data[i].startTime, 'Y') == app.formatTime(data[i].endTime, 'Y')) {
          data[i].endMonthTime = app.formatTime(data[i].endTime, 'M.D')
        } else {
          data[i].endMonthTime = app.formatTime(data[i].endTime, 'Y.M.D')
        }
        data[i].startMonthTime = app.formatTime(data[i].startTime, 'y.M.d')
        data[i].saleTime = app.formatTime(data[i].timingSaleTime, 'M月d日 h:m')

        if (app.formatTime(data[i].startTime, 'YMD') == app.formatTime(data[i].endTime, 'YMD')) {
          data[i].isCommonDay = true
        } else {
          data[i].isCommonDay = false
        }
        if (data[i].img.indexOf("http") == 0) {
        } else {
          data[i].img = app.globalData.base_image_url + data[i].img
        }
        rewardData.push(data[i])
      }
      console.log(data)
      that.setData({
        rewardList: rewardData
      })
    })
  },

  goDetail: function (e) {
    console.log(e.currentTarget.dataset.id)
     console.log(e.currentTarget.dataset.id)
    var nextUrl = './detail/detail?id=' + e.currentTarget.dataset.id
    wx.navigateTo({
      url: nextUrl,
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      rewardList: [],
      currentPage: 1
    })
    this.getRewardListData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      currentPage: this.data.currentPage+1
    })
    this.getRewardListData()
  },












  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})